//
//  ViewController.m
//  PhotoBrowser
//
//  Created by Macmini on 16/12/1.
//  Copyright © 2016年 Macmini. All rights reserved.
//

#import "ViewController.h"
#import "MYCollectionViewCell.h"

@interface ViewController ()<UIImagePickerControllerDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UINavigationControllerDelegate>
{
    NSMutableArray<UIImage *> *data;
}
@property (strong, nonatomic) UICollectionView *collectionView;
@property (strong, nonatomic) UICollectionViewFlowLayout *layout;
@end

@implementation ViewController

static NSString * const identifier = @"cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    data = [NSMutableArray new];
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:self.layout];
    self.collectionView.backgroundColor = [UIColor darkGrayColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[MYCollectionViewCell class] forCellWithReuseIdentifier:identifier];
    
    self.layout.minimumLineSpacing = 1;
    self.layout.minimumInteritemSpacing = 1;
    self.layout.itemSize = CGSizeMake((self.view.frame.size.width - 3) / 3 , (self.view.frame.size.width - 3) / 3);

    
    UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(onClick:)];
    self.navigationItem.rightBarButtonItem = barBtn;
    self.title = @"UI";
    
    
}

- (void)onClick:(id)sender
{
    UIImagePickerController *con = [[UIImagePickerController alloc] init];
    con.delegate = self;
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选取图片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"从相册选择" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        con.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:con animated:YES completion:nil];
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    
    UIImage *imag = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    [data addObject:imag];
    [self.collectionView reloadData];
    NSLog(@"count:%d",data.count);
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return data.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MYCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    cell.imgVi.image = data[indexPath.item];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
